﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TPCN.Commons
{
    public class StringHelper
    {
        public static string NameToUrl(string name)
        {
            string tmp = String.Empty;
            Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            tmp = Regex.Replace(name, "[^\\w\\s]", string.Empty).Replace(" ", "-").ToLower();
            string strFormD = tmp.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, string.Empty).Replace("đ", "d");
        }

        public static string CutString(string str, int length)
        {
            if (str.Length > length)
            {
                string s = str.Substring(0, length);
                string[] arr = s.Split(' ');
                string returnstr = String.Empty;
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    returnstr += arr[i] + " ";
                }
                return returnstr + "...";
            }
            return str;
        }

        public static string GetNameOfEmail(string email)
        {
            return email.Remove(email.IndexOf("@"));
        }
    }
}
